package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxCollectVo;
import com.cskaoyan.service.WxCollectService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author gatsby
 * @since 2022/10/14 10:44
 */
@RestController
@RequestMapping("wx/collect")
public class WxCollectController {

    @Autowired
    WxCollectService wxCollectService;

    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo, Short type) {
        CommonData<WxCollectVo> commonData = wxCollectService.queryCollectByTypeLimit(basePageInfo, type);
        return BaseRespVo.ok(commonData, "成功");
    }


    @RequestMapping("addordelete")
    public BaseRespVo addordelete(@RequestBody Map map) {

        //获取用户名
        //Session session = SecurityUtils.getSubject().getSession();
        //HttpSession session = request.getSession();
        //String username = (String)session.getAttribute("username");

        //TODO:这里先写死
        // String username="user123";

        // Subject subject = SecurityUtils.getSubject();
        // MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        // Integer userId = marketUser.getId();
        //String username = marketUser.getUsername();
        //wxCollectService.addOrDeleteCollect(map,username);

        //获取用户信息
        Subject subject = SecurityUtils.getSubject();
        //判断用户是否已经登录
        if (subject.isAuthenticated()) {
            wxCollectService.addOrDeleteCollect(map);
        } else {
            return new BaseRespVo(null, "请登录", 501);
        }


        return BaseRespVo.ok("成功");


    }

}
